<?php
class Nav_model extends CI_Model {

    var $nav_view;
	
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();

	}

	function get_nav($sel = NULL, $post_name = NULL)
	{
		$this->load->model('db_model');
		$this->db_model->loadTable('post_type');
		$post_type_array = $this->db_model->findAll(NULL, NULL, 'order');		
		
		$this->db_model->loadTable('post');
		
		$post_type_name_array = array();
		foreach($post_type_array as $post_type)
		{
			// if the type no record, won't show the nav
			$post = $this->db_model->findCount(array('typeid'=>$post_type->id));
			if($post > 0)				
				$post_type_name_array['post/index/' . $post_type->name . '/' . $post_type->id] = $post_type->name;	
		}
		
		$home_href = site_url('home');
		if($sel == 'home')
		{
			
			$this->nav_view = '<li><a href="'.$home_href.'" id="sel">首页</a></li>';
			
			foreach($post_type_name_array as $href=>$nav_name)
			{
				$href_new = site_url($href);
				$this->nav_view .= '<li><a href="'.$href_new.'">'.$nav_name.'</a></li>';
			}
			
		}
		else
		{
			$this->nav_view = '<li><a href="'.$home_href.'">首页</a></li>';
			
			foreach($post_type_name_array as $href=>$nav_name)
			{
				$href_new = site_url($href);
				if(urlencode($nav_name) == $post_name)
					$this->nav_view .= '<li><a href="'.$href_new.'" id="sel">'.$nav_name.'</a></li>';
				else
					$this->nav_view .= '<li><a href="'.$href_new.'">'.$nav_name.'</a></li>';
			}
		}
		

	   
		return $this->nav_view;
	}
}
